﻿Imports System
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Runtime.Tracking
    <Serializable> _
    Public NotInheritable Class UserTrackingLocation
        ' Methods
        Public Sub New()
            Me._conditions = New TrackingConditionCollection
        End Sub

        Public Sub New(ByVal argumentTypeName As String)
            Me._conditions = New TrackingConditionCollection
            Me._argName = argumentTypeName
        End Sub

        Public Sub New(ByVal argumentType As Type)
            Me._conditions = New TrackingConditionCollection
            Me._argType = argumentType
        End Sub

        Public Sub New(ByVal argumentTypeName As String, ByVal activityTypeName As String)
            Me._conditions = New TrackingConditionCollection
            Me._argName = argumentTypeName
            Me._activityName = activityTypeName
        End Sub

        Public Sub New(ByVal argumentTypeName As String, ByVal activityType As Type)
            Me._conditions = New TrackingConditionCollection
            Me._argName = argumentTypeName
            Me._activityType = activityType
        End Sub

        Public Sub New(ByVal argumentType As Type, ByVal activityTypeName As String)
            Me._conditions = New TrackingConditionCollection
            Me._argType = argumentType
            Me._activityName = activityTypeName
        End Sub

        Public Sub New(ByVal argumentType As Type, ByVal activityType As Type)
            Me._conditions = New TrackingConditionCollection
            Me._argType = argumentType
            Me._activityType = activityType
        End Sub

        Private Function ActTypeIsMatch(ByVal activity As Activity) As Boolean
            If (Not Me._activityType Is Nothing) Then
                Return TypeMatch.IsMatch(activity, Me._activityType, Me._trackDerivedActivities)
            End If
            Return TypeMatch.IsMatch(activity, Me._activityName, Me._trackDerivedActivities)
        End Function

        Private Function ConditionsAreMatch(ByVal obj As Object) As Boolean
            Dim condition As TrackingCondition
            For Each condition In Me._conditions
                If Not condition.Match(obj) Then
                    Return False
                End If
            Next
            Return True
        End Function

        Friend Function Match(ByVal activity As Activity) As Boolean
            If Not Me.ActTypeIsMatch(activity) Then
                Return False
            End If
            Return Me.ConditionsAreMatch(activity)
        End Function

        Friend Function Match(ByVal activity As Activity, ByVal keyName As String, ByVal arg As Object) As Boolean
            Return Me.RuntimeMatch(activity, keyName, arg)
        End Function

        Private Function RuntimeMatch(ByVal activity As Activity, ByVal keyName As String, ByVal obj As Object) As Boolean
            If Not Me.ActTypeIsMatch(activity) Then
                Return False
            End If
            If ((Not Me._keyName Is Nothing) AndAlso (String.Compare(Me._keyName, keyName, StringComparison.Ordinal) <> 0)) Then
                Return False
            End If
            If (Not Me._argType Is Nothing) Then
                Return TypeMatch.IsMatch(obj, Me._argType, Me._trackDerivedArgs)
            End If
            Return TypeMatch.IsMatch(obj, Me._argName, Me._trackDerivedArgs)
        End Function


        ' Properties
        Public Property ActivityType As Type
            Get
                Return Me._activityType
            End Get
            Set(ByVal value As Type)
                Me._activityType = value
            End Set
        End Property

        Public Property ActivityTypeName As String
            Get
                Return Me._activityName
            End Get
            Set(ByVal value As String)
                Me._activityName = value
            End Set
        End Property

        Public Property ArgumentType As Type
            Get
                Return Me._argType
            End Get
            Set(ByVal value As Type)
                Me._argType = value
            End Set
        End Property

        Public Property ArgumentTypeName As String
            Get
                Return Me._argName
            End Get
            Set(ByVal value As String)
                Me._argName = value
            End Set
        End Property

        Public ReadOnly Property Conditions As TrackingConditionCollection
            Get
                Return Me._conditions
            End Get
        End Property

        Public Property KeyName As String
            Get
                Return Me._keyName
            End Get
            Set(ByVal value As String)
                Me._keyName = value
            End Set
        End Property

        Public Property MatchDerivedActivityTypes As Boolean
            Get
                Return Me._trackDerivedActivities
            End Get
            Set(ByVal value As Boolean)
                Me._trackDerivedActivities = value
            End Set
        End Property

        Public Property MatchDerivedArgumentTypes As Boolean
            Get
                Return Me._trackDerivedArgs
            End Get
            Set(ByVal value As Boolean)
                Me._trackDerivedArgs = value
            End Set
        End Property


        ' Fields
        Private _activityName As String
        Private _activityType As Type
        Private _argName As String
        Private _argType As Type
        Private _conditions As TrackingConditionCollection
        Private _keyName As String
        Private _trackDerivedActivities As Boolean
        Private _trackDerivedArgs As Boolean
    End Class
End Namespace

